package com.hejia.alauda.service;

import com.hejia.alauda.entity.RedEnvelope;
import com.hejia.alauda.entity.Redpacket;
import com.hejia.alauda.entity.example.RedEnvelopeExample;
import com.hejia.alauda.entity.example.RedpacketExample;
import com.hejia.alauda.persistence.mybatis.RedEnvelopeMapper;
import com.hejia.alauda.persistence.mybatis.RedpacketMapper;
import com.hejia.alauda.utils.Pager;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 
 * 
 *
 *
 * Created by showone on 2014 下午1:11:01.
 */
@Service
public class RedEnvelopeServiceImpl implements RedEnvelopeService {

	@Resource
	private RedEnvelopeMapper mapper;

	@Resource
	private RedpacketMapper redpacketMapper;

	/**
	 * 创建红包
	 * 
	 * @param redEnvelope
	 *            红包
	 * @return
	 */
	public boolean createRedEnvelope(RedEnvelope redEnvelope) {

		return mapper.insert(redEnvelope) > 0;
	}

	/**
	 * 根据红包ID获取红包
	 * 
	 * @param id
	 *            红包ID
	 * @return
	 */
	public RedEnvelope getRedEnvelopeById(Long id) {
		return mapper.selectByPrimaryKey(id);
	}

	/**
	 * 根据红包接收人ID获取红包列表
	 * 
	 * @param id
	 *            红包ID
	 * @return
	 */
	public List<RedEnvelope> getRedEnvelopesBySendeeId(Long sendeeId) {
		RedEnvelopeExample exp = new RedEnvelopeExample();
		exp.appendCriterion("sendeeId=", sendeeId);

		return mapper.selectByExample(exp, null);
	}

	@Override
	public Pager<Redpacket> findAllRedpacket(Pager<Redpacket> pager) {
		return findAllRedpacket(pager, new RedpacketExample());
	}

	@Override
	public Pager<Redpacket> findAllRedpacket(Pager<Redpacket> pager,
			RedpacketExample example) {
		example.setOrderByClause("id desc");
		pager.setList(redpacketMapper.selectByExample(example, pager));
		pager.setTotalCount(redpacketMapper.countByExample(example));
		return pager;
	}

	@Override
	public boolean saveRedpacket(Redpacket redpacket) {
		redpacket.setCreateDate(new Date());
		redpacket.setModifyDate(new Date());
		return redpacketMapper.insert(redpacket) > 0;
	}

	@Override
	public Redpacket getRedpacketById(Long id) {
		return redpacketMapper.selectByPrimaryKey(id);
	}

	@Override
	public boolean modifyRedpacket(Redpacket redpacket) {
		Redpacket r = redpacketMapper.selectByPrimaryKey(redpacket.getId());
		r.setModifyDate(new Date());
		r.setName(redpacket.getName());
		r.setCode(redpacket.getCode());
		r.setValue(redpacket.getValue());
		r.setEffectDate(redpacket.getEffectDate());
		r.setInvalidDate(redpacket.getInvalidDate());
		r.setBackground(redpacket.getBackground());
		r.setInstruction(redpacket.getInstruction());
        r.setAvailableTime(redpacket.getAvailableTime());
        r.setWithdrawLimit(redpacket.getWithdrawLimit());
        r.setCashRatio(redpacket.getCashRatio());
        r.setIntroduction(redpacket.getIntroduction());
		return redpacketMapper.updateByPrimaryKeyWithNullValue(r) > 0;
	}

	@Override
	public boolean deleteRedpacket(Long id) {
		Redpacket redpacket = redpacketMapper.selectByPrimaryKey(id);
		return redpacketMapper.deleteByPrimaryKey(redpacket) > 0;
	}

	@Override
	public Redpacket getRedpacketByCode(String code) {
		RedpacketExample exp = new RedpacketExample();
		exp.appendCriterion("code=", code);

		return redpacketMapper.selectOneByExample(exp);
	}

	@Override
	public List<Redpacket> findAllRedpackets() {
		return redpacketMapper.selectByExample(new RedpacketExample(), null);
	}
}
